package com.lenovo.dil.tiltbaseddemo.utils;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class DisplayUtils {
	public static final String SCREENWIDTH = "screenWidth";
	public static final String SCREENHEIGHT = "screenHeight";

	/**
	 * 默认屏幕宽
	 */
	public static final int defaultWidth = 480;
	/**
	 * 默认屏幕高
	 */
	public static final int defaultHeight = 854;

	public static float getDensity(Context context) {
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		return dm.density;
	}

	private static int densityDpi = -1;

	public static float px2dp(Context context, float px) {
		initDensity(context);
		float dips = (px * 160f) / densityDpi;
		return dips;
	}

	public static float dp2px(Context context, float dp) {
		initDensity(context);
		float pixs = dp * (densityDpi / 160f);
		return pixs;
	}

	public static int getDensityDpi(Context context) {
		initDensity(context);
		return densityDpi;
	}

	private static void initDensity(Context context) {
		if (-1 == densityDpi) {
			WindowManager wm = (WindowManager) context
					.getSystemService(Service.WINDOW_SERVICE);
			DisplayMetrics dm = new DisplayMetrics();
			wm.getDefaultDisplay().getMetrics(dm);
			densityDpi = dm.densityDpi;
		}
	}

	private DisplayUtils() {
		// 减少无意识的实例化操作
	}

	/** * 判断是否开启了自动亮度调节 */

	public static boolean isAutoBrightness(Activity activity) {
		boolean automicBrightness = false;
		try {
			automicBrightness = Settings.System.getInt(
					activity.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		}
		return automicBrightness;
	}

	// 然后就是要觉得当前的亮度了，这个就比较纠结了：

	/** * 获取屏幕的亮度 */

	public static int getScreenBrightness(Activity activity) {
		int nowBrightnessValue = 0;
		try {
			nowBrightnessValue = android.provider.Settings.System.getInt(
					activity.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS);
		} catch (Exception e) {
			e.printStackTrace();

		}
		return nowBrightnessValue;
	}

	// 那如何修改屏幕的亮度呢？

	/** * 设置亮度 */

	public static void setBrightness(Activity activity, int brightness) {

		if (brightness < 5) {
			brightness = 5;
		}
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
		activity.getWindow().setAttributes(lp);
	}

	// 那么，能设置了，但是为什么还是会出现，设置了，没反映呢？

	// 嘿嘿，那是因为，开启了自动调节功能了，那如何关闭呢？这才是最重要的：

	/** * 停止自动亮度调节 */

	public static void stopAutoBrightness(Activity activity) {

		Settings.System.putInt(activity.getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
	}

	// 能开启，那自然应该能关闭了哟哟，那怎么关闭呢？很简单的：

	/**
	 * * 开启亮度自动调节 *
	 * 
	 * @param activity
	 */

	public static void startAutoBrightness(Activity activity) {

		Settings.System.putInt(activity.getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);

	}

	// 至此，应该说操作亮度的差不多都有了，结束！
	// 哎，本来认为是应该结束了，但是悲剧得是，既然像刚才那样设置的话，只能在当前的activity中有作用，一段退出的时候，会发现毫无作用，悲剧，原来是忘记了保存了。汗！

	/** * 保存亮度设置状态 */

	public static void saveBrightness(Activity activity, int brightness) {
		if (brightness < 5) {
			brightness = 5;
		}
		Uri uri = android.provider.Settings.System
				.getUriFor("screen_brightness");

		android.provider.Settings.System.putInt(activity.getContentResolver(),
				"screen_brightness", brightness);

		// resolver.registerContentObserver(uri, true, myContentObserver);

		activity.getContentResolver().notifyChange(uri, null);
	}
}
